import { Response } from '@/types/Response';
import axiosInstance from './index'

/**
 * 请求，包装一层，这里做拦截器特殊情况的处理
 * @returns 
 */
export const wrapRequest = (eject: boolean = false) => {
    const instance = axiosInstance();
    const requestInterceptor = instance.interceptors.request.use(
		(config) => {
			// 请求拦截器
            //todo  请求头token。。。
            console.log("=====");
			return config;
		},
		(err) => {
			return Promise.reject(err);
		}
	);
	const responseInterceptor = instance.interceptors.response.use(
		(response) => {
			// 响应拦截器
            // 响应判断：提示信息等
			return response;
		},
		(err) => {
			return Promise.reject(err);
		}
    );
    // 如果传入eject 则删除当前拦截器
    if (eject) {
        instance.interceptors.request.eject(requestInterceptor);
        instance.interceptors.response.eject(responseInterceptor);
    }
    return instance;
}


class Http{
	service;
	constructor(instance: any) {
		this.service = instance;
	}
	// T 是响应信息具体数据
	get<T>(url: string, params?: object, _object = {}): Promise<Response<T>>{
        return this.service.get(url, { ...params, ..._object })
	}
	post<T>(url: string, params?: object, _object = {}): Promise<Response<T>>{
		return this.service.post(url, params, _object);
	}
	put<T>(url: string, params?: object, _object = {}): Promise<Response<T>> { 
		return this.service.put(url, params, _object);
	}
	delete<T>(url: string, params?: object, _object = {}): Promise<Response<T>> { 
		return this.service.delete(url, { ...params, ..._object });
	}
}

// 默认使用这个请求，如果有其他需求可以使用wrapRequest进行包装处理
export const instance = wrapRequest();
export default new Http(instance);